﻿Public Class nuevo_investigador
    Dim qry As String
    Dim id As String
    'consulta editar
    Dim ceditar As String
    Dim flag As Boolean = False
    Dim dataset As DataSet
    Public Sub setid(ByVal id As Integer)
        Me.id = id
        flag = True
    End Sub
   
    Private Sub TextBox3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
        If ValidarRut(TextBox3.Text, TextBox3, ErrorProvider1) = False Or ValidaExistenteins(TextBox3.Text, TextBox3, ErrorProvider1) = False Then
            TextBox3.Focus()
        End If
     
    End Sub

    Private Sub btnAceptar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_aceptar.Click
        Dim idi As String
        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            qry = "insert into investigadores (inve_nombre, inve_apellido, inve_rut,inve_centro,inve_telefono,inve_email,inve_ciudad) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "')"
            qry = qry & " ;SELECT @@Identity AS Indentity"
            Consulta(qry, dataset)
            idi = dataset.Tables("data").Rows(0)(0)
            Try

            Catch ex As SqlClient.SqlException
                MsgBox("Error: " & ex.Message)
            End Try
            'registro movimiento
            registro("agregar", login.usuario.id, main.modulo.investigador)
            MsgBox("Investigador agregado", MsgBoxStyle.OkOnly)
            Me.Close()

        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub
    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub
    Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub
    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub
    Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub nuevo_investigador_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Icon = (My.Resources.favicon)
        If (flag) Then
            Me.Text = "Editar Investigador"
            subtitulo.Text = "Editar Investigador"
            btn_aceptar.Hide()
            btn_editar.Show()
            'carga de datos en textbox
            ceditar = "select * from investigadores where inve_id='" & id & "'"
            Consulta(ceditar, dataset)
            TextBox1.Text = dataset.Tables("data").Rows(0)(3)
            TextBox2.Text = dataset.Tables("data").Rows(0)(4)
            TextBox3.Text = dataset.Tables("data").Rows(0)(5)
            TextBox4.Text = dataset.Tables("data").Rows(0)(6)
            TextBox5.Text = dataset.Tables("data").Rows(0)(7)
            TextBox6.Text = dataset.Tables("data").Rows(0)(8)
            TextBox7.Text = dataset.Tables("data").Rows(0)(9)
        Else
            btn_editar.Hide()
        End If
    End Sub

    Private Sub btn_editar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_editar.Click
        Dim qry As String

        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            qry = "update investigadores set inve_nombre='" & TextBox1.Text & "',inve_apellido='" & TextBox2.Text & "',inve_rut='" & TextBox3.Text & "',inve_centro='" & TextBox4.Text & "',inve_telefono='" & TextBox5.Text & "',inve_email='" & TextBox6.Text & "',inve_ciudad='" & TextBox7.Text & "' where inve_id='" & id & "'"
            'EjecutaConsulta(qry)
            Me.Close()
            Try
                EjecutaConsulta(qry)
                man_investigadores.actualizar()
            Catch ex As SqlClient.SqlException
                MsgBox("Error: " & ex.Message)
            End Try
            'registro movimiento
            registro("modificar", login.usuario.id, main.modulo.usuario)
        End If
    End Sub

End Class